#include "../coredef.h"
#include "cppdef.h"


/*--------------------------------------------------------------*/
/* CPreprocessing用グローバル変数											*/
/*--------------------------------------------------------------*/
#define CPP_GNL_OPTION_SIZE 1024
typedef struct _INITMACRO
{
	PSZ		pszMacroName;
	PSZ		pszRepStr;
	U2	usKind;
	U2			usModifiable;
	U4	usCplKind ;
} INITMACRO;
static  INITMACRO stInitMacroInfo[ ] = {
	{	"__DATE__",				NULL	,	MACRO_KIND_STD|MACRO_KIND_DATE	,	MACRO_UNCHANGEABLE	,	CPLKIND_ALL		},
	{	"__TIME__",				NULL	,	MACRO_KIND_STD|MACRO_KIND_TIME	,	MACRO_UNCHANGEABLE	,	CPLKIND_ALL		},
	{	"__FILE__",				NULL	,	MACRO_KIND_STD|MACRO_KIND_FILE	,	MACRO_UNCHANGEABLE	,	CPLKIND_ALL		},
	{	"__LINE__",				NULL	,	MACRO_KIND_STD|MACRO_KIND_LINE	,	MACRO_UNCHANGEABLE	,	CPLKIND_ALL		},
	{	"__STDC__",				"1"		,	MACRO_KIND_STD|MACRO_KIND_STDC	,	MACRO_CHANGEABLE	,	CPLKIND_ALL		},
	{	"__BASE__",				NULL	,	MACRO_KIND_STD|MACRO_KIND_SRC	,	MACRO_UNCHANGEABLE	,	CPLKIND_ALL		},
	{	"__FUNCTION__",			NULL	,	MACRO_KIND_STD|MACRO_KIND_FUNC	,	MACRO_UNCHANGEABLE	,	CPLKIND_ALL		},
	{	NULL,					NULL	,	MACRO_KIND_NONE		,				MACRO_UNCHANGEABLE	,	0				}
};
static S4 cppMainAddOptMacro( U2 optKind, PSZ pszOptStr );
static VOID cppMainResetPreDefMacro( VOID );
/******************************************************************
* 関 数 名 : cppCloseMem
* 機    能 :
* 引    数 : VOID
* 戻 り 値 : S4
* 	　　　:１．　正常終了時：	COEXIT_SUCCESS
* 　　　　　 ：２．　致命エラー：	COEXIT_FATAL
* 詳細内容 :
*******************************************************************/
static VOID cppCloseMem()
{
	stCppDirectiveIFGroupAccess.reset();
	stCppDisableMacroAccess.reset();
	stCppNestFileInfoAccess.reset();
	stCppIncDirAccess.reset();
	stCppMacroCtrlAccess.reset();
	stCppMcrArgInfoAccess.reset();
	stCppLabelStrAccess.reset();
	stCppMcrPrmLabelAccess.reset();
	stCppTokenAccess.reset();
	stCppReadBufferAccess.reset();
	stCppMcrExtStkAccess.reset();
}


/******************************************************************
* 関 数 名 : cppOpenMem
* 機    能 :
* 引    数 : VOID
* 戻 り 値 : S4
* 	　　　:１．　正常終了時：	COEXIT_SUCCESS
* 　　　　　 ：２．　致命エラー：	COEXIT_FATAL
* 詳細内容 :
*******************************************************************/
static S4 cppOpenMem()
{
	/*　初期値設定　*/
	BOOL nRet = TRUE;
	AST  nKindMax;

	stCppLabelStrAccess.clear();
	stCppReadBufferAccess.clear();
	stCppDirectiveIFGroupAccess.clear();
	stCppDisableMacroAccess.clear();
	stCppNestFileInfoAccess.clear();
	stCppIncDirAccess.clear();
	stCppMacroCtrlAccess.clear();
	stCppMcrArgInfoAccess.clear();
	stCppMcrExtStkAccess.clear();
	stCppMcrPrmLabelAccess.clear();
	stCppTokenAccess.clear();

	stCorePPConditionAccess.clear();
	stCorePPTokenListAccess.clear();
	stCoreDirectiveInfoAccess.clear();
	stCoreDefRepInfoAccess.clear();
	stCoreMacroDefInfoAccess.clear();
	nRet = stCppDirectiveIFGroupAccess.alloc(1);
	if( nRet == FALSE )
	{
		return COEXIT_FATAL;
	}
	/* 処理フラッグTRUEを追加する */
	nKindMax = stCppDirectiveIFGroupAccess.getMax();
	stCppDirectiveIFGroupAccess.state.set( nKindMax, TRUE );
	return ( COEXIT_SUCCESS );
}


/******************************************************************
* 関 数 名 : cppProcOption
* 機    能 :
* 引    数 : VOID
* 戻 り 値 : S4
* 	　　　:１．　正常終了時：	COEXIT_SUCCESS
* 　　　　　 ：２．　致命エラー：	COEXIT_FATAL
* 	   :3.  エラー終了         COEXIT_ERROR
* 詳細内容 :
*******************************************************************/
static S4 cppProcOption( VOID )
{
	/*　初期値設定　*/
	AST astCnt;
	AST astMax = stCoreOptAccess.getMax();
	U2 optId;
	S4 nRet, result = COEXIT_SUCCESS;
	PSZ pszOptStr;
	AST nKindMax;
	for( astCnt = MIN_ARRAY; astCnt <= astMax; astCnt ++ )
	{
		optId = stCoreOptAccess.optId.get(astCnt);
		pszOptStr = stCoreOptAccess.strValue.get( astCnt );
		switch( optId )
		{
		case	OPTID_CMPLKIND	:
			nRet = MpuProcOption( stCoreOptAccess.strValue.get(astCnt) );
			if( nRet != COEXIT_SUCCESS )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optId );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optId ), &temp );
				result = nRet > result?nRet:result;
			}
			break;
		case	OPTID_PTRDIFF_TYPE	:
		case	OPTID_SIZEOF_TYPE	:
		case	OPTID_ENUMTYPE	:
		case	OPTID_CHAR_UNSIGNED	:
		case	OPTID_BIT_UNSIGNED	:
		case	OPTID_BIT_SIGNED	:
		case	OPTID_NEARFAR	:
		case	OPTID_SHIFT_BITNUM_REMAINDER	:
		case	OPTID_SHIFT_BITNUM_ALL	:
		case	OPTID_SHIFT_KIND_ARITH	:
		case	OPTID_SHIFT_KIND_LOGICAL	:
		case	OPTID_MODULO_SIGNED	:
		case	OPTID_MODULO_UNSIGNED	:
		case	OPTID_BREAK_PPNUM	:
		case	OPTID_BIT_TYPE	:
			/* ccのオプション  */
			break;
		case	OPTID_CPPCOMMENT	:
			stCoreEnvInfo.ucCppComment = TRUE;
			break;
		case	OPTID_CPPRESULT	:
			stCoreEnvInfo.ucCppResult = TRUE;
			break;
		case	OPTID_SRC_MACRO	:
			stCoreEnvInfo.ucSrcNameMcr = TRUE;
			break;
		case	OPTID_FUNCTION_MACRO	:
			stCoreEnvInfo.ucFuncNameMcr = TRUE;
			break;
		case	OPTID_MAX_ALIGNMENT	:
		case	OPTID_BITBYTE_NEW	:
		case	OPTID_BYTEBIT_NEW	:
		case	OPTID_BITBIT_BYSIZE	:
		case	OPTID_DIVIDE_BIT	:
		case	OPTID_ZEROBIT	:
		case	OPTID_BITBIT_ARRAGNE	:
		case	OPTID_POINTERSIZE	:
		case	OPTID_NEAR_POINTERSIZE	:
		case	OPTID_FAR_POINTERSIZE	:
		case	OPTID_SHORTSIZE	:
		case	OPTID_INTSIZE	:
		case	OPTID_LONGSIZE	:
		case	OPTID_FLOATSIZE	:
		case	OPTID_DBLSZIE	:
		case	OPTID_LONGDBLSIZE	:
		case	OPTID_BUILDINFUNC	:
			break;
		case	OPTID_I_SYM	:
		case	OPTID_I_USER	:
		case	OPTID_DMACRO	:
		case	OPTID_UMACRO	:
		case	OPTID_NMACRO	:
			break;
		case    OPTID_C90:
			stCoreEnvInfo.ucC90 = TRUE;
			stCoreEnvInfo.ucC99 = FALSE;
			stCoreEnvInfo.ucGNU = FALSE;
			break;
		case    OPTID_C99:
			stCoreEnvInfo.ucC90 = FALSE;
			stCoreEnvInfo.ucC99 = TRUE;
			stCoreEnvInfo.ucGNU = FALSE;
			break;
		case    OPTID_GNU:
			stCoreEnvInfo.ucC90 = FALSE;
			stCoreEnvInfo.ucC99 = FALSE;
			stCoreEnvInfo.ucGNU = TRUE;
			break;
		default:
			result = COEXIT_ERROR;
			break;
		}
	}
	cppMainResetPreDefMacro();
	for( astCnt = MIN_ARRAY; astCnt <= astMax; astCnt ++ )
	{
		optId = stCoreOptAccess.optId.get(astCnt);
		switch( optId )
		{
		case	OPTID_CMPLKIND	:
		case	OPTID_PTRDIFF_TYPE	:
		case	OPTID_SIZEOF_TYPE	:
		case	OPTID_ENUMTYPE	:
		case	OPTID_CHAR_UNSIGNED	:
		case	OPTID_BIT_UNSIGNED	:
		case	OPTID_BIT_SIGNED	:
		case	OPTID_NEARFAR	:
		case	OPTID_SHIFT_BITNUM_REMAINDER	:
		case	OPTID_SHIFT_BITNUM_ALL	:
		case	OPTID_SHIFT_KIND_ARITH	:
		case	OPTID_SHIFT_KIND_LOGICAL	:
		case	OPTID_MODULO_SIGNED	:
		case	OPTID_MODULO_UNSIGNED	:
		case	OPTID_BREAK_PPNUM	:
		case	OPTID_BIT_TYPE	:
		case	OPTID_CPPCOMMENT	:
		case	OPTID_CPPRESULT	:
		case	OPTID_SRC_MACRO	:
		case	OPTID_FUNCTION_MACRO	:
			break;
		case	OPTID_MAX_ALIGNMENT	:
		case	OPTID_BITBYTE_NEW	:
		case	OPTID_BYTEBIT_NEW	:
		case	OPTID_BITBIT_BYSIZE	:
		case	OPTID_DIVIDE_BIT	:
		case	OPTID_ZEROBIT	:
		case	OPTID_BITBIT_ARRAGNE	:
		case	OPTID_POINTERSIZE	:
		case	OPTID_NEAR_POINTERSIZE	:
		case	OPTID_FAR_POINTERSIZE	:
		case	OPTID_SHORTSIZE	:
		case	OPTID_INTSIZE	:
		case	OPTID_LONGSIZE	:
		case	OPTID_FLOATSIZE	:
		case	OPTID_DBLSZIE	:
		case	OPTID_LONGDBLSIZE	:
		case	OPTID_BUILDINFUNC	:
			break;
		case	OPTID_I_SYM	:
			pszOptStr = stCoreOptAccess.strValue.get(astCnt);
			if( COEXIT_NFOUND == FileCheckDir( pszOptStr ) )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optId );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optId ), &temp );
				result = COEXIT_ERROR > result?COEXIT_ERROR:result;
				break;
			}
			if( FALSE == stCppIncDirAccess.alloc(1) )
			{
				result = COEXIT_FATAL;
				break;
			}
			nKindMax = stCppIncDirAccess.getMax();
			stCppIncDirAccess.name.set( nKindMax, pszOptStr );
			stCppIncDirAccess.kind.set( nKindMax, OPT_INC_SYS );
			break;
		case	OPTID_I_USER	:
			pszOptStr = stCoreOptAccess.strValue.get(astCnt);
			if( COEXIT_NFOUND == FileCheckDir( pszOptStr ) )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optId );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optId ), &temp );
				result = COEXIT_ERROR > result?COEXIT_ERROR:result;
				break;
			}
			if( FALSE == stCppIncDirAccess.alloc(1) )
			{
				result = COEXIT_FATAL;
				break;
			}
			nKindMax = stCppIncDirAccess.getMax();
			stCppIncDirAccess.name.set( nKindMax, pszOptStr );
			stCppIncDirAccess.kind.set( nKindMax, OPT_INC_USR );
			break;
		case	OPTID_DMACRO	:
			pszOptStr = stCoreOptAccess.strValue.get(astCnt);
			if( 0 == str_length( pszOptStr ) )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optId );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optId ), &temp );
				result = COEXIT_ERROR > result?COEXIT_ERROR:result;
				break;
			}
			nRet = cppMainAddOptMacro( OPT_DEFMCR, pszOptStr );
			if( nRet != COEXIT_SUCCESS )
			{
				stCoreOptAccess.optId.set(astCnt,OPTID_NONE);
				result = nRet > result?nRet:result;
			}
			break;
		case	OPTID_UMACRO	:
			pszOptStr = stCoreOptAccess.strValue.get(astCnt);
			if( 0 == str_length( pszOptStr ) )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optId );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optId ), &temp );
				result = COEXIT_ERROR > result?COEXIT_ERROR:result;
				break;
			}
			nRet = cppMainAddOptMacro( OPT_UNDEFMCR, pszOptStr );
			if( nRet != COEXIT_SUCCESS )
			{
				stCoreOptAccess.optId.set(astCnt,OPTID_NONE);
				result = nRet > result?nRet:result;
			}
			break;
		case	OPTID_NMACRO	:
			pszOptStr = stCoreOptAccess.strValue.get(astCnt);
			if( 0 == str_length( pszOptStr ) )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optId );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optId ), &temp );
				result = COEXIT_ERROR > result?COEXIT_ERROR:result;
				break;
			}
			nRet = cppMainAddOptMacro( OPT_DISABLED_MCR, pszOptStr );
			if( nRet != COEXIT_SUCCESS )
			{
				stCoreOptAccess.optId.set(astCnt,OPTID_NONE);
				result = nRet > result?nRet:result;
			}
			break;
		case    OPTID_C90:
			break;
		case    OPTID_C99:
			break;
		case    OPTID_GNU:
			break;
		default:
			result = COEXIT_ERROR;
			break;
		}
	}
	return result;
}
/******************************************************************
* 関 数 名 : cppMainResetPreDefMacro
* 機    能 : 予約マクロの属性をリセットする
* 引    数 : VOID
* 戻 り 値 : VOID
* 詳細内容 :
*******************************************************************/
static VOID cppMainResetPreDefMacro( VOID )
{
	S4 i,j;
	for(i = 0; stInitMacroInfo[i].pszMacroName != NULL; i++)
	{
		if( stInitMacroInfo[i].usKind & MACRO_KIND_SRC )
		{
			if( stCoreEnvInfo.ucSrcNameMcr )
			{
				for( j = 0; stIndefinableMacro[j].pszMacroName != NULL; j++ )
				{
					if( strcmp( stIndefinableMacro[j].pszMacroName, stInitMacroInfo[i].pszMacroName ) == 0 )
					{
						stIndefinableMacro[j].bValid = 1;
						break;
					}
				}
				stInitMacroInfo[i].usModifiable = MACRO_UNCHANGEABLE;
			}
			else
			{
				stInitMacroInfo[i].usModifiable = MACRO_CHANGED;
			}
		}
		else if ( stInitMacroInfo[i].usKind & MACRO_KIND_FUNC )
		{
			if( stCoreEnvInfo.ucFuncNameMcr )
			{
				for( j = 0; stIndefinableMacro[j].pszMacroName != NULL; j++ )
				{
					if( strcmp( stIndefinableMacro[j].pszMacroName, stInitMacroInfo[i].pszMacroName ) == 0 )
					{
						stIndefinableMacro[j].bValid = 1;
						break;
					}
				}
				stInitMacroInfo[i].usModifiable = MACRO_UNCHANGEABLE;
			}
			else
			{
				stInitMacroInfo[i].usModifiable = MACRO_CHANGED;
			}
		}
		else if ( stInitMacroInfo[i].usKind & MACRO_KIND_STDC )
		{
			stInitMacroInfo[i].usModifiable = MACRO_CHANGEABLE;
		}
	}
}
/******************************************************************
* 関 数 名 : cppMainAddOptMacro
* 機    能 : オプションをマクロに追加
* 引    数 : VOID
* 戻 り 値 : S4
* 	　　　		COEXIT_SUCCESS			正常終了
*			COEXIT_ERROR			エラー終了
*			COEXIT_FATAL			致命エラー
* 詳細内容 :
*******************************************************************/
static S4 cppMainAddOptMacro(  U2 optKind, PSZ pszOptStr  )
{
	S4 nRetVal    = 0 ;
	S4 i          = 0 ;
	PSZ    pszMacroName = NULL;
	PSZ    pszTemp;
	BOOL	bRet;
	AST    astDisableMcr;
	U4    u4_McrLen;
	CHAR   chTmpBuffer[CPP_GNL_BUFFER_SIZE+8] = {0};
	/*オプションの文字列を取得*/
	if( str_length( pszOptStr ) >= CPP_GNL_OPTION_SIZE)
	{
		ALERT_MSG_PARAM temp;
		temp.opt = CCoreGetOptKey( optKind );
		temp.param = pszOptStr;
		temp.next = 0;
		CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
		return COEXIT_ERROR;
	}
	pszMacroName = libStrSkipSpace(pszOptStr);
	u4_McrLen = libStrGetSymbolLength(pszMacroName);
	if( u4_McrLen == 0 )
	{
		return COEXIT_ERROR;
	}
	pszOptStr = pszMacroName + u4_McrLen;
	if(optKind == OPT_DEFMCR)
	{
		strcpy (chTmpBuffer,"#define ");
	}
	else
	{
		strcpy (chTmpBuffer,"#undef ");
	}
	pszTemp = &chTmpBuffer[strlen(chTmpBuffer)];
	strncpy(&chTmpBuffer[strlen(chTmpBuffer)], pszMacroName, u4_McrLen );
	pszMacroName = pszTemp;

	for(i = 0; stInitMacroInfo[i].pszMacroName != NULL; i++)
	{
		/*オプションの文字列の先頭からstInitMacroInfoに登録したマクロ名と比較*/
		nRetVal = strcmp(pszMacroName,stInitMacroInfo[i].pszMacroName);

		/*文字列,コンパイラ種類の比較*/
		if(   ( nRetVal == 0 )
		   && (stCoreEnvInfo.ulCplKind & stInitMacroInfo[i].usCplKind))
		{
			break;
		}
	}

	strcat(chTmpBuffer," ");
	if( stInitMacroInfo[i].pszMacroName != NULL )
	{
		if(optKind == OPT_DEFMCR)       /* 属性USHORT_OPTKINDはOPTID_DEFMCRの場合（－D) */
		{
			if(stInitMacroInfo[i].usModifiable == MACRO_UNCHANGEABLE)
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optKind );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
				return COEXIT_ERROR;
			}
			else
			{
				stInitMacroInfo[i].usModifiable = MACRO_CHANGED ;
			}
		}
		else if(optKind == OPT_UNDEFMCR) /* 属性USHORT_OPTKINDはOPTID_DEFMCRの場合（－U) */
		{
			if((stInitMacroInfo[i].usKind & MACRO_KIND_STD) != 0)
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optKind );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
				return COEXIT_ERROR;
			}
			else
			{
				stInitMacroInfo[i].usModifiable = MACRO_CHANGED;
			}
		}
	}
	else
	{
		for(i = 0; stIndefinableMacro[i].pszMacroName != NULL; i++)
		{
			/*オプションの文字列の先頭からstInitMacroInfoに登録したマクロ名と比較*/
			nRetVal = strcmp(pszMacroName,stIndefinableMacro[i].pszMacroName);

			/*文字列,コンパイラ種類の比較*/
			if( nRetVal == 0 )
			{
				break;
			}
		}
		if( stIndefinableMacro[i].pszMacroName != NULL )
		{
			if(optKind == OPT_DEFMCR)
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optKind );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
				return COEXIT_ERROR;
			}
			else if(optKind == OPT_UNDEFMCR) /* 属性USHORT_OPTKINDはOPTID_DEFMCRの場合（－U) */
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optKind );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
				return COEXIT_ERROR;
			}
		}
	}
	if(optKind == OPT_DISABLED_MCR)
	{
		bRet = stCppDisableMacroAccess.alloc(1);
		if(bRet != TRUE )
		{
			return COEXIT_FATAL;
		}
		astDisableMcr = stCppDisableMacroAccess.getMax();
		stCppDisableMacroAccess.name.set( astDisableMcr, pszMacroName );
	}
	/*オプションの文字列を"#define ***"の形に転換*/
	else
	{
		pszOptStr = libStrSkipSpace(pszOptStr);
		if( OPT_DEFMCR == optKind )
		{
			if( *pszOptStr != '\0' && *pszOptStr != '=')
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optKind );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
				return COEXIT_ERROR;
			}
			strcat(chTmpBuffer," ");
			if( *pszOptStr == '=' )
			{
				pszOptStr++;
				pszOptStr = libStrSkipSpace(pszOptStr);
				if( str_length( pszOptStr ) > 0 )
				{
					strcat(chTmpBuffer,pszOptStr);
				}
			}
			else
			{
				strcat(chTmpBuffer,"1");
			}
		}
		else
		{
			if( *pszOptStr != '\0' )
			{
				ALERT_MSG_PARAM temp;
				temp.opt = CCoreGetOptKey( optKind );
				temp.param = pszOptStr;
				temp.next = 0;
				CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
				return COEXIT_ERROR;
			}
		}
		nRetVal = CppCtlReqMain(chTmpBuffer);
		/*戻り値をリセット*/
		if(nRetVal == COEXIT_FATAL)
		{
			return  COEXIT_FATAL;
		}
		else if(nRetVal != COEXIT_SUCCESS )
		{
			ALERT_MSG_PARAM temp;
			temp.opt = CCoreGetOptKey( optKind );
			temp.param = pszOptStr;
			temp.next = 0;
			CppThrowWarning( CPP_OPT_ILLEGAL_PARAM, 0, CCoreGetOptKey( optKind ), &temp );
			return nRetVal;
		}
	}

	return (COEXIT_SUCCESS);
}


/******************************************************************
* 関 数 名 : cppMainAddPreDefMacro
* 機    能 :
* 引    数 : VOID
* 戻 り 値 : S4
* 	　　　		COEXIT_SUCCESS			正常終了
*			COEXIT_ERROR			エラー終了
*			COEXIT_FATAL			致命エラー
* 詳細内容 :
*******************************************************************/
static S4 cppMainAddPreDefMacro( VOID )
{
	/* 戻り値設定 */
	S4  nRetVal    = 0 ;
	S4  nRet       = COEXIT_SUCCESS ;
	S4  i          = 0 ;
	AST	 astMaxKind;
	U2	usKind;
	CHAR chTmpBuffer[CPP_GNL_BUFFER_SIZE] = {0} ;

	for(i = 0; stInitMacroInfo[i].pszMacroName != NULL; i++)
	{
		/* マクロの修正可能なフラグはMACRO_CHANGEDの場合,次にマクロから処理 */
		if (stInitMacroInfo[i].usModifiable == MACRO_CHANGED) {
			continue;
		}

		/* コンパイラ種類は今のコンパイラ種類と違う場合,次にマクロから処理 */
		if ((stCoreEnvInfo.ulCplKind & stInitMacroInfo[i].usCplKind) == 0)
		{
			continue;
		}

		/* DEF ファイルを"#define ***"の形に転換 */
		strcpy(chTmpBuffer, "#define ");
		strcat(chTmpBuffer, stInitMacroInfo[i].pszMacroName);
		strcat(chTmpBuffer, " ");

		/* 置換内容を追加 */
		if(stInitMacroInfo[i].pszRepStr != NULL )
		{
			strcat(chTmpBuffer, stInitMacroInfo[i].pszRepStr);
		}
		else
		{
                        /* -Dで指定されたら、1とする 11/06 コメント追加*/
			strcat(chTmpBuffer, "1" );
		}
		strcat(chTmpBuffer, "\n" );
		/* ディフィニション文を解析*/
		nRetVal = CppCtlReqMain(chTmpBuffer);

		/*戻り値をリセット*/
		if(nRetVal == COEXIT_FATAL)
		{
			nRet = COEXIT_FATAL ;
		}
		else if(nRetVal > nRet )
		{
			nRet = nRetVal;
		}

		/* MACROKIND設定 */
		astMaxKind = stCoreMacroDefInfoAccess.getMax();
		if( astMaxKind >= MIN_ARRAY )
		{
			usKind = stInitMacroInfo[i].usKind | stCoreMacroDefInfoAccess.macroKind.get(astMaxKind);
			stCoreMacroDefInfoAccess.macroKind.set(astMaxKind, usKind);
		}
	}

	return (nRet);
}

/******************************************************************
* 関 数 名 : CPreprocessing
* 機    能 :
* 引    数 : PSZ pszSrc
* 戻 り 値 : S4
* 	　　　:	1 COEXIT_SUCCESS		正常終了
*			2 COEXIT_WARNING		警告
*			3 COEXIT_ERROR		コンパイルエラー
*			4 COEXIT_FATAL		致命エラー
* 詳細内容 :
*******************************************************************/
S4 DLL_EXPORT CPreprocessing( PSZ pszSrc )
{
	/*　初期値設定　*/
	S4 nRet = COEXIT_SUCCESS ;
	if( COEXIT_SUCCESS != FileInitialize( ) )
	{
		return COEXIT_FATAL;
	}
	/* メモリオープン */
	nRet = cppOpenMem();
	if( nRet != COEXIT_SUCCESS )
	{
		cppCloseMem();
		return( nRet );
	}
	nRet = cppProcOption();
	if( nRet != COEXIT_SUCCESS )
	{
		cppCloseMem();
		return( nRet );
	}
	/* 予約マクロ追加 */
	nRet = cppMainAddPreDefMacro();
	if( nRet != COEXIT_SUCCESS )
	{
		cppCloseMem();
		return( nRet );
	}

	/* カレントディレクトリ取得 */
	nRet = GetFullPath( pszSrc, szCurrentDirectory, OS_FULLPATH );
	if( nRet != TRUE )
	{
		cppCloseMem();
		return( nRet );
	}
	/* 前処理メイン　*/
	nRet = CppCtrlMain( szCurrentDirectory );
	/* 中間テーブルを閉じる */
	cppCloseMem();

	/* 戻る */
	if (nRet == COEXIT_FINISHED || nRet == COEXIT_FILEEND) {
		nRet = COEXIT_SUCCESS;
	}
	return( nRet );
}
EXTERNAL_SYMBOL( CPreprocessing );
VOID DLL_EXPORT ClearTUPreprocessingResult( VOID ){
	stCoreDirectiveInfoAccess.clear();
	stCoreDefRepInfoAccess.clear();
	stCoreMacroDefInfoAccess.clear();
	stCorePPConditionAccess.clear();
	stCorePPTokenListAccess.clear();
}
EXTERNAL_SYMBOL( ClearTUPreprocessingResult );
VOID  CppThrowWarning( U4 code, AST astCodeScp, PSZ pszParameter, ALERT_MSG_PARAM *szValue ){
	S4 errcode = CCoreThrowWarning( code, astCodeScp, pszParameter, szValue );
	gblErrorFlag = errcode > gblErrorFlag? errcode : gblErrorFlag;
}


/*  end of file */
